import VPlay 2.0
import QtQuick 2.0
import "scenes"

GameWindow {
  id: window  
  width: 960
  height: 640
  
  // You get free licenseKeys from http://v-play.net/licenseKey
  // With a licenseKey you can:
  //  * Publish your games & apps for the app stores
  //  * Remove the V-Play Splash Screen or set a custom one (available with the Pro Licenses)
  //  * Add plugins to monetize, analyze & improve your apps (available with the Pro Licenses)
  //licenseKey: "<generate one from http://v-play.net/licenseKey>"

  // create and remove entities at runtime
  EntityManager {
    id: entityManager
  }

  // menu scene
  MenuScene {
    id: menuScene
    // listen to the button signals of the scene and change the state according to it
    onSelectLevelPressed: window.state = "selectLevel"
    onCreditsPressed: window.state = "credits"
    // the menu scene is our start scene, so if back is pressed there we ask the user if he wants to quit the application
    onBackButtonPressed: {
      nativeUtils.displayMessageBox(qsTr("Really quit the game?"), "", 2);
    }

    // listen to the return value of the MessageBox
    Connections {
      target: nativeUtils
      onMessageBoxFinished: {
        // only quit, if the activeScene is menuScene - the messageBox might also get opened from other scenes in your code
        if(accepted && window.activeScene === menuScene)
          Qt.quit()
      }
    }
  }

  // scene for selecting levels
  SelectLevelScene {
    id: selectLevelScene
    onLevelPressed: {
      // selectedLevel is the parameter of the levelPressed signal
      gameScene.setLevel(selectedLevel)
      window.state = "game"
    }
    onRobotPressed: {
      gameScene.setRobot(selectedLevel, ip)
      //console.log("onRobotPressed ip = " + ip)
      window.state = "game"
    }
    onRobotDisconnect: {
      gameScene.exit(disconnect_ip)
    }

    onBackButtonPressed: window.state = "menu"
  }

  // credits scene
  CreditsScene {
    id: creditsScene
    onBackButtonPressed: window.state = "menu"
  }

  // game scene to play a level
  GameScene {
    id: gameScene
    onBackButtonPressed: window.state = "selectLevel"

    onSettingPressed: window.state = "setting"
  }

  Setting {
    id: setting
    onBackButtonPressed: window.state = "game"
    onSendPathCmd: gameScene.sendPathCmd(cmd, number, dir)
  }

  // menuScene is our first scene, so set the state to menu initially
  state: "menu"
  activeScene: menuScene

  // state machine, takes care reversing the PropertyChanges when changing the state, like changing the opacity back to 0
  states: [
    State {
      name: "menu"
      PropertyChanges {target: menuScene; opacity: 1}
      PropertyChanges {target: window; activeScene: menuScene}
    },
    State {
      name: "selectLevel"
      PropertyChanges {target: selectLevelScene; opacity: 1}
      PropertyChanges {target: window; activeScene: selectLevelScene}
    },
    State {
      name: "credits"
      PropertyChanges {target: creditsScene; opacity: 1}
      PropertyChanges {target: window; activeScene: creditsScene}
    },
    State {
      name: "game"
      PropertyChanges {target: gameScene; opacity: 1}
      PropertyChanges {target: window; activeScene: gameScene}
    },
    State {
      name: "setting"
      PropertyChanges {target: setting; opacity: 1}
      PropertyChanges {target: window; activeScene: setting}
    }
  ]
}
